package com.itheima.controller;

import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.feign.client.UserFeignClient;
import org.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("consumer")
public class ConsumerController {

    @Autowired
    UserFeignClient userFeignClient;

    @RequestMapping("{id}")
    public User findById(@PathVariable("id") String id){
        return userFeignClient.findById(id);
    }


//
//
//    @Autowired
//    RestTemplate restTemplate;
//
//    @Autowired
//    DiscoveryClient discoveryClient;
//
//    @GetMapping("{id}")
//    @HystrixCommand(fallbackMethod = "queryByIdDallBack")   //服务降级
//    public Map findBuId(@PathVariable("id") String id){
//        if ("itheima".equals(id)){
//            throw new RuntimeException("干饭人");
//        }
//
//
//        //调用用户微服务  获取用户信息
//        //RestTemplate 是spring封装的http客户端模板对象
//        return restTemplate.getForObject("http://user-service/user/"+id,Map.class);
//    }
//
//    public Map queryByIdDallBack(String id){
//        Map map = new HashMap<>();
//
//        map.put("msg","网咯错误，请稍后访问");
//        return map;
//
//    }





//    @GetMapping("{id}")
//    public Map findBuId(@PathVariable("id") String id){
//        //根据名称获取服务注册列表
//        List<ServiceInstance> instances =
//                discoveryClient.getInstances("user-service");
//        //获取服务列表第一个服务
//        String host = instances.get(0).getHost();
//        System.out.println("host = " + host);
//        int port = instances.get(0).getPort();
//        //调用用户微服务  获取用户信息
//        //RestTemplate 是spring封装的http客户端模板对象
//        return restTemplate.getForObject("http://"+host+":"+port+"/user/"+id,Map.class);
//    }

}
